public class Solution01{
	public int sumNumbers(TreeNode root) {
		return dfs(root, 0);
    }

    /** 前序遍历 */
	private int dfs(TreeNode root, int path){
        if(root == null){ //出口
            return 0;
        }

        // 根
        path = path * 10 + root.val;
        if(root.left == null && root.right == null){
            return path;
        }

        // 左
        int pathLeft = dfs(root.left, path);
        // 右
        int pathRight = dfs(root.right, path);
        return pathLeft + pathRight;
	}
}

class TreeNode {
    int val;
    TreeNode left;
    TreeNode right;
    TreeNode() {}
    TreeNode(int val) { this.val = val; }
    TreeNode(int val, TreeNode left, TreeNode right) {
        this.val = val;
        this.left = left;
        this.right = right;
    }
}
